<template>
  <div class="login-container">
    <StarfieldBackground 
      :star-count="100"
      :connection-distance="150"
      :interactive="true"
      :mouse-radius="120"
      :mouse-power="8"
      :meteor-count="2"
      :show-mouse-area="false"
    />
    <dv-full-screen-container>
      <div class="login-bg">
        <div class="login-box">
          <div class="login-header">
            <h1 class="login-title">赣地元低空应用服务平台</h1>
            <p class="login-subtitle">Ganjiang Low-altitude Application Service Platform</p>
          </div>
          
          <div class="login-form">
            <el-form 
              ref="loginFormRef" 
              :model="loginForm" 
              :rules="loginRules"
              @submit.prevent="handleLogin"
            >
              <el-form-item prop="username">
                <el-input
                  v-model="loginForm.username"
                  placeholder="请输入用户名"
                  size="large"
                  prefix-icon="User"
                  clearable
                />
              </el-form-item>
              
              <el-form-item prop="password">
                <el-input
                  v-model="loginForm.password"
                  type="password"
                  placeholder="请输入密码"
                  size="large"
                  prefix-icon="Lock"
                  show-password
                  @keyup.enter="handleLogin"
                />
              </el-form-item>
              
              <el-form-item>
                <el-button 
                  type="primary" 
                  size="large" 
                  :loading="loading"
                  @click="handleLogin"
                  class="login-btn"
                >
                  {{ loading ? '登录中...' : '登录' }}
                </el-button>
              </el-form-item>
            </el-form>
          </div>
          
          <div class="login-footer">
            <p>默认账号: adminPC / adminPC</p>
          </div>
        </div>
      </div>
    </dv-full-screen-container>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/modules/user'
import { ElMessage } from 'element-plus'
import StarfieldBackground from '@/components/StarfieldBackground.vue'

const router = useRouter()
const userStore = useUserStore()

const loginFormRef = ref()
const loading = ref(false)

const loginForm = reactive({
  username: '',
  password: ''
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  await loginFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        const result = await userStore.login(loginForm.username, loginForm.password)
        if (result.success) {
          ElMessage.success(result.message)
          router.push('/dashboard')
        } else {
          ElMessage.error(result.message)
        }
      } catch (error) {
        ElMessage.error('登录失败，请重试')
      } finally {
        loading.value = false
      }
    }
  })
}

onMounted(() => {
  // 检查是否已登录
  userStore.checkAuth()
})
</script>

<style lang="scss" scoped>
.login-container {
  width: 100vw;
  height: 100vh;
  background: #0a0e1a;
  position: relative;
  overflow: hidden;
  
  :deep(.dv-full-screen-container) {
    width: 100%;
    height: 100%;
  }
}

.login-bg {
  width: 100%;
  height: 100vh;
  position: relative;
}

.login-box {
  width: 450px;
  background: #0B1325;
  border: 1px solid rgba(0, 153, 255, 0.3);
  border-radius: 12px;
  padding: 40px;
  backdrop-filter: blur(10px);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 10;
  
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 2px;
    background: linear-gradient(90deg, transparent, #00d4ff, transparent);
  }
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
  
  .login-title {
    font-size: 24px;
    color: #00d4ff;
    margin-bottom: 10px;
    font-weight: bold;
    text-shadow: 0 0 10px rgba(0, 212, 255, 0.5);
  }
  
  .login-subtitle {
    font-size: 14px;
    color: #8fa8cc;
    opacity: 0.8;
  }
}

.login-form {
  :deep(.el-form-item) {
    margin-bottom: 24px;
  }
  
  :deep(.el-input) {
    .el-input__wrapper {
      background: #0B1325;
      border: 1px solid rgba(0, 153, 255, 0.3);
      border-radius: 6px;
      box-shadow: none;
      
      &:hover {
        border-color: rgba(0, 153, 255, 0.5);
      }
      
      &.is-focus {
        border-color: #00d4ff;
        box-shadow: 0 0 0 2px rgba(0, 212, 255, 0.2);
      }
    }
    
    .el-input__inner {
      color: #ffffff;
      background-color: transparent !important;
      -webkit-text-fill-color: #ffffff !important;
      
      &::placeholder {
        color: #8fa8cc;
      }
      
      &:-webkit-autofill,
      &:-webkit-autofill:hover,
      &:-webkit-autofill:focus,
      &:-webkit-autofill:active {
        -webkit-box-shadow: inset 0 0 0 1000px #0B1325 !important;
        box-shadow: inset 0 0 0 1000px #0B1325 !important;
        -webkit-text-fill-color: #ffffff !important;
        caret-color: #ffffff !important;
        transition: background-color 5000s ease-in-out 0s;
      }
    }
  }
  
  .login-btn {
    width: 100%;
    height: 44px;
    background: linear-gradient(135deg, #00d4ff 0%, #0066cc 100%);
    border: none;
    border-radius: 6px;
    font-size: 16px;
    font-weight: bold;
    transition: all 0.3s;
    
    &:hover {
      background: linear-gradient(135deg, #00a8cc 0%, #0052a3 100%);
      transform: translateY(-2px);
      // box-shadow: 0 4px 12px rgba(0, 212, 255, 0.4);
    }
    
    &:active {
      transform: translateY(0);
    }
  }
}

.login-footer {
  text-align: center;
  margin-top: 30px;
  
  p {
    color: #8fa8cc;
    font-size: 12px;
    opacity: 0.8;
  }
}

.decoration {
  position: absolute;
  border-radius: 50%;
  opacity: 0.08;
  backdrop-filter: blur(10px);
  
  &.decoration-1 {
    width: 200px;
    height: 200px;
    background: radial-gradient(circle, rgba(0, 212, 255, 0.3), transparent);
    top: 10%;
    left: 10%;
    animation: float 6s ease-in-out infinite;
  }
  
  &.decoration-2 {
    width: 150px;
    height: 150px;
    background: radial-gradient(circle, rgba(0, 255, 136, 0.3), transparent);
    top: 60%;
    right: 15%;
    animation: float 8s ease-in-out infinite reverse;
  }
  
  &.decoration-3 {
    width: 100px;
    height: 100px;
    background: radial-gradient(circle, rgba(255, 107, 107, 0.3), transparent);
    bottom: 20%;
    left: 20%;
    animation: float 10s ease-in-out infinite;
  }
}

@keyframes float {
  0%, 100% {
    transform: translateY(0px);
  }
  50% {
    transform: translateY(-20px);
  }
}

// 响应式设计
@media (max-width: 768px) {
  .login-box {
    width: 90%;
    padding: 30px 20px;
  }
  
  .login-header .login-title {
    font-size: 20px;
  }
}
</style>